{% extends '../_manage.html' %}

{% block title %} {{ form.name }} {% endblock %}

{% block head %}

<script>

$(function() {
    var id = '{{ id }}';
    if (id) {
        getJSON('/api/adslots/' + id, function (err, adslot) {
            if (err) {
                return showError(err);
            }
            initVM(adslot);
        });
    }
    else {
        initVM({
            id: ''
        });
    }
});

function initVM(adslot) {
    var vm = new Vue({
        el: '#vm',
        data: {
            updateDisabled: adslot.id !== '',
            name: adslot.name || '',
            alias: adslot.alias || '',
            description: adslot.description || '',
            price: adslot.price || 1000,
            width: adslot.width || 336,
            height: adslot.height || 280,
            num_slots: adslot.num_slots || 3,
            num_auto_fill: adslot.num_auto_fill || 0,
            auto_fill: adslot.auto_fill || ''
        },
        created: function () {
            $('#loading').hide();
            $('#vm').show();
        },
        methods: {
            submit: function() {
                var that = this;
                clearFormError();
                that.$resource('{{ form.action }}').save(this.$data).then(function (resp) {
                    resp.json().then(function (result) {
                        location.assign('{{ form.redirect }}');
                    });
                }, onJsonFormError);
            }
        }
    });
}
</script>

{% endblock %}

{% block main %}

    <div id="loading" class="uk-width-1-1">
        <i class="uk-icon-spinner uk-icon-spin"></i> {{ _('Loading') }}...
    </div>

    <div id="vm" class="uk-width-1-1">
        <form v-on:submit.prevent="submit" class="uk-margin uk-form uk-form-stacked">
            <legend>{{ form.name }}</legend>
            <fieldset>
                <div class="uk-alert uk-alert-danger uk-hidden"></div>
                <div class="uk-form-row">
                    <label class="uk-form-label">{{ _('Name') }}:</label>
                    <div class="uk-form-controls">
                        <input v-model="name" name="name" type="text" maxlength="100" class="uk-width-1-1">
                    </div>
                </div>
                <div class="uk-form-row">
                    <label class="uk-form-label">{{ _('Description') }}:</label>
                    <div class="uk-form-controls">
                        <input v-model="description" name="description" type="text" maxlength="100" class="uk-width-1-1">
                    </div>
                </div>
                <div class="uk-form-row">
                    <label class="uk-form-label">{{ _('Alias') }}:</label>
                    <div class="uk-form-controls">
                        <input v-bind:disabled="updateDisabled" v-model="alias" name="alias" type="text" maxlength="100" class="uk-width-1-1">
                    </div>
                </div>
                <div class="uk-form-row">
                    <label class="uk-form-label">{{ _('Price') }}:</label>
                    <div class="uk-form-controls">
                        <input v-model.number="price" name="price" type="text" maxlength="100" class="uk-width-1-1">
                    </div>
                </div>
                <div class="uk-form-row">
                    <label class="uk-form-label">{{ _('Width') }}:</label>
                    <div class="uk-form-controls">
                        <input v-bind:disabled="updateDisabled" v-model.number="width" name="width" type="text" maxlength="100" class="uk-width-1-1">
                    </div>
                </div>
                <div class="uk-form-row">
                    <label class="uk-form-label">{{ _('Height') }}:</label>
                    <div class="uk-form-controls">
                        <input v-bind:disabled="updateDisabled" v-model.number="height" name="height" type="text" maxlength="100" class="uk-width-1-1">
                    </div>
                </div>
                <div class="uk-form-row">
                    <label class="uk-form-label">{{ _('Number of slots') }}:</label>
                    <div class="uk-form-controls">
                        <input v-model.number="num_slots" name="num_slots" type="text" maxlength="100" class="uk-width-1-1">
                    </div>
                </div>
                <div class="uk-form-row">
                    <label class="uk-form-label">{{ _('Number of auto fill') }}:</label>
                    <div class="uk-form-controls">
                        <input v-model.number="num_auto_fill" name="num_auto_fill" type="text" maxlength="100" class="uk-width-1-1">
                    </div>
                </div>
                <div class="uk-form-row">
                    <label class="uk-form-label">{{ _('Auto fill') }}:</label>
                    <div class="uk-form-controls">
                        <textarea v-model="auto_fill" name="auto_fill" rows="16" class="uk-width-1-1 x-codearea"></textarea>
                    </div>
                </div>
                <div class="uk-form-row">
                    <div class="uk-form-controls">
                        <button type="submit" class="uk-button uk-button-primary"><i class="uk-icon-check"></i> {{ _('Save') }}</button>
                        <button type="button" class="uk-button" onclick="location.assign('{{ form.redirect }}')"><i class="uk-icon-times"></i> {{ _('Cancel') }}</button>
                    </div>
                </div>
            </fieldset>
        </form>
    </div>

{% endblock %}
